package com.xiexin.web.controller.setmeals;

import com.alibaba.fastjson.JSON;
import com.xiexin.common.annotation.Log;
import com.xiexin.common.core.controller.BaseController;
import com.xiexin.common.core.domain.AjaxResult;
import com.xiexin.common.core.page.TableDataInfo;
import com.xiexin.common.enums.BusinessType;
import com.xiexin.common.utils.poi.ExcelUtil;
import com.xiexin.setmeals.domain.Setmeal;
import com.xiexin.setmeals.service.ISetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 套餐信息Controller
 *
 * @author xiexin
 * @date 2022-03-25
 */
@RestController
@RequestMapping("/setmeals/setmeals")
public class SetmealController extends BaseController {
    @Autowired
    private ISetmealService setmealService;

    /**
     * 新增套餐
     */
    @PreAuthorize("@ss.hasPermi('setmeal:setmeal:add')")
   // @Log(title = "套餐", businessType = BusinessType.INSERT)
    @RequestMapping(value ="/addSetmealAndIds", method = {RequestMethod.POST} ,produces = "application/json")
   // @PostMapping("/addSetmealAndIds")
    public AjaxResult addSetmealAndIds(@RequestBody Map data) {
        System.out.println(" 进来了。。" );
        List groupIds = JSON.parseObject(data.get("groupIds") + "", List.class);
        System.out.println("groupIds = " + groupIds);
        Setmeal setmeal = JSON.parseObject(JSON.toJSONString(data.get("setmeal")), Setmeal.class);
        System.out.println("setmeal = " + setmeal);
        return toAjax(setmealService.insertSetmeal(setmeal, groupIds));
    }


    /**
     * 查询套餐信息列表
     */
    @PreAuthorize("@ss.hasPermi('setmeals:setmeals:list')")
    @GetMapping("/list")
    public TableDataInfo list(Setmeal setmeal) {
        startPage();
        List<Setmeal> list = setmealService.selectSetmealList(setmeal);
        return getDataTable(list);
    }

    /**
     * 导出套餐信息列表
     */
    @PreAuthorize("@ss.hasPermi('setmeals:setmeals:export')")
    @Log(title = "套餐信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Setmeal setmeal) {
        List<Setmeal> list = setmealService.selectSetmealList(setmeal);
        ExcelUtil<Setmeal> util = new ExcelUtil<Setmeal>(Setmeal.class);
        util.exportExcel(response, list, "套餐信息数据");
    }

    /**
     * 获取套餐信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('setmeals:setmeals:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(setmealService.selectSetmealById(id));
    }

    /**
     * 新增套餐信息
     */
    @PreAuthorize("@ss.hasPermi('setmeals:setmeals:add')")
    @Log(title = "套餐信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Setmeal setmeal) {
        return toAjax(setmealService.insertSetmeal(setmeal));
    }

    /**
     * 修改套餐信息
     */
    @PreAuthorize("@ss.hasPermi('setmeals:setmeals:edit')")
    @Log(title = "套餐信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Setmeal setmeal) {
        return toAjax(setmealService.updateSetmeal(setmeal));
    }

    /**
     * 删除套餐信息
     */
    @PreAuthorize("@ss.hasPermi('setmeals:setmeals:remove')")
    @Log(title = "套餐信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(setmealService.deleteSetmealByIds(ids));
    }
}
